<?php

class Athlete {

    public $id;
    public $first_name;
    public $last_name;
    public $dob;
    public $gender;
    public $team_id;
    public $birthYear;
    
    public $team;

    function __construct($first_name = '', $last_name = '', $dob = '', $gender = '', $team_id = '') {
        $this->first_name = $first_name;
        $this->last_name = $last_name;
        $this->dob = $dob;
        $this->gender = $gender;
        $this->team_id = $team_id;
    }

    /**
     * Load Athlete from db by ID.
     * @param int $id
     * @return Athlete
     */
    public static function fromID($id) {
        $instance = new self();
        if ($instance->loadByID($id) === false)
            return NULL;
        return $instance;
    }

    /**
     * Load Athlete by database row.
     * @param array $row
     * @return Athlete
     */
    public static function fromRow(array $row) {
        $instance = new self();
        $instance->fill($row);
        return $instance;
    }

    protected function loadByID($id) {
        $result = mysql_query("SELECT `id`, `first_name`, `last_name`, `dob`, `gender`, `team_id` FROM `athletes` WHERE `id`='$id'") or die(mysql_errno());
        if (mysql_num_rows($result) == 0) {
            return false;
        }
        $row = mysql_fetch_assoc($result);
        $this->fill($row);
    }

    protected function fill(array $row) {
        $this->id = $row['id'];
        $this->first_name = $row['first_name'];
        $this->last_name = $row['last_name'];
        $this->dob = $row['dob'];
        $this->gender = $row['gender'];
        $this->team_id = $row['team_id'];
    }

    public function toArray() {
        return array($this->first_name, $this->last_name, $this->dob, $this->gender, $this->team_id);
    }

    public function getID() {
        return $this->id;
    }

    public function getFirstName() {
        return $this->first_name;
    }

    public function setFirstName($first_name) {
        $this->first_name = $first_name;
    }

    public function getLastName() {
        return $this->last_name;
    }

    public function setLastName($last_name) {
        $this->last_name = $last_name;
    }

    public function getDob() {
        return $this->dob;
    }

    public function setDob($dob) {
        $this->dob = $dob;
    }

    public function getGender() {
        return $this->gender;
    }

    public function setGender($gender) {
        $this->gender = $gender;
    }

    public function getTeamID() {
        return $this->team_id;
    }

    public function setTeamID($team_id) {
        $this->team_id = $team_id;
    }
    
    /**
     * 
     * @return Team
     */
     public function getTeam() {
        return $this->team;
    }
    
    public function setTeam(Team $team) {
        $this->team = $team;
    }

    public function getBirthYear() {
        return date('y', strtotime($this->dob));
    }
    
    public function getBirthYearLong() {
        return date('Y', strtotime($this->dob));
    }

    public function __toString() {
        return "first_name: $this->first_name, last_name: $this->last_name, gender: $this->gender, dob: $this->dob<br>";
    }

    public function toTableRow() {
        return "<tr><td>$this->first_name</td><td>$this->last_name</td><td>$this->gender</td><td>$this->dob</td></tr>";
    }

    public function toTableColumns() {
        return "<td>$this->first_name</td><td>$this->last_name</td><td>$this->gender</td><td>$this->dob</td>";
    }

}

?>
